<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 顶部搜索和添加区域 -->
      <el-row :gutter="30">
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            v-model="queryInfo.query"
            clearable
            @clear='getOrdersList'
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getOrdersList"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button
            type="primary"
            @click="addOrders"
          >添加订单</el-button>
        </el-col>
      </el-row>

      <!-- 订单表格区域 -->
      <el-table
        :data='orders_list'
        border
        stripe
      >
        <el-table-column type="index"></el-table-column>
        <el-table-column
          label="订单编号"
          prop="order_number"
        ></el-table-column>
        <el-table-column
          label="订单价格（元）"
          prop="order_price"
          width="120"
        ></el-table-column>
        <el-table-column
          label="是否付款"
          prop="pay_status"
          width="120"
        >
          <template slot-scope="scope">
            <el-tag
              type="success"
              v-if="scope.row.pay_status !== '0'"
            >已付款</el-tag>
            <el-tag
              type="danger"
              v-else
            >未付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="是否发货"
          prop="is_send"
          width="100"
        >
        </el-table-column>
        <el-table-column
          label="下单时间"
          prop="create_time"
          width="180"
        >
          <template slot-scope="scope">
            {{scope.row.create_time | dateFormat}}
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          width="180"
        >
          <template>
            <el-button
              type="primary"
              size="mini"
              @click="addressBoxShow"
            >编辑</el-button>
            <el-button
              type="danger"
              size="mini"
              @click="kuaidiBoxShow"
            >物流</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <div class="block">
        <span class="demonstration"></span>
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.pagenum"
          :page-sizes="[1, 5, 10, 20]"
          :page-size="10"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </el-card>

    <!-- 编辑地址弹窗 -->
    <el-dialog
      title="编辑信息"
      :visible.sync="addressVisible"
      width="50%"
      @close="handleCloseDialog"
    >
      <el-form
        :model="addressForm"
        :rules="addressRules"
        ref="addressRef"
        label-width="100px"
      >
        <el-form-item
          label="省市区/县"
          prop="address1"
        >
          <el-cascader
            v-model="addressForm.address1"
            :options="citydata"
          ></el-cascader>
        </el-form-item>
        <el-form-item
          label="详细地址"
          prop="address2"
        >
          <el-input v-model="addressForm.address2"></el-input>
        </el-form-item>
      </el-form>
      <span
        slot="footer"
        class="dialog-footer"
      >
        <el-button @click="addressVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="addressVisible = false"
        >确 定</el-button>
      </span>
    </el-dialog>

    <!-- 物流信息弹窗 -->
    <el-dialog
      title="物流信息"
      :visible.sync="kuaidiVisible"
      width="50%"
      @close="kuaidiCloseDialog"
    >
      <el-timeline :reverse="reverse">
        <el-timeline-item
          v-for="(activity, index) in kuaidiInfo"
          :key="index"
          :timestamp="activity.time"
        >
          {{activity.context}}
        </el-timeline-item>
      </el-timeline>
    </el-dialog>
  </div>
</template>

<script>
import citydata from './citydata'

export default {
  data () {
    return {
      // 请求数据需要的参数
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      orders_list: [],
      total: 0,
      addressForm: {
        address1: [],
        address2: ''
      },
      addressVisible: false,
      addressRules: {
        address1: [{ required: true, message: '请选择正确的地址', trigger: 'blur' }],
        address2: [{ required: true, message: '请输入正确的地址', trigger: 'blur' }]
      },
      citydata,
      // 物流信息本地数据
      kuaidiVisible: false,
      kuaidiInfo: [],
      reverse: false // 时间线倒序
    }
  },
  created () {
    this.getOrdersList()
  },
  methods: {
    // 获取订单数据界面
    async getOrdersList () {
      const { data: res } = await this.$http.get('orders', { params: this.queryInfo })
      // console.log(res)
      this.orders_list = res.data.goods
      this.total = res.data.total
    },
    // 添加订单事件
    addOrders () { },
    // 点击删除按钮弹出删除确认弹窗
    delDialogShow () { },
    // 分页变化
    handleSizeChange (newSize) {
      this.queryInfo.pagesize = newSize
      this.getOrdersList()
    },
    handleCurrentChange (newNum) {
      this.queryInfo.pagenum = newNum
      this.getOrdersList()
    },
    // 控制地址弹窗弹出
    addressBoxShow () {
      this.addressVisible = true
    },
    // 地址弹窗关闭时清空本地数据
    handleCloseDialog () {
      this.addressForm.address1 = []
      this.addressForm.address2 = ''
    },
    // 物流弹窗关闭时清空数据
    kuaidiCloseDialog () { },
    // 点击按钮弹出物流信息弹窗
    async kuaidiBoxShow () {
      this.kuaidiVisible = true
      const { data: res } = await this.$http.get('/kuaidi/1106975712662')
      if (res.meta.status !== 200) return this.$message.error('获取物流信息失败')
      // console.log(res)
      this.kuaidiInfo = res.data
      // console.log(this.kuaidiInfo)
    }
  }
}
</script>

<style>
</style>
